;(function() {

	define(['angular', 'uiBootstrap', 'jbox'], function(angular) {

		var app = angular.module('app', ['ui.bootstrap']), islogin = false;

		app.factory('safeApply', ['$rootScope', function($rootScope) {//apply
		    return function(scope, fn) {
		        var phase = scope.$root.$$phase;
		        if (phase == '$apply' || phase == '$digest') {
		            if (fn && ( typeof (fn) === 'function')) fn();
		        } else {
		            scope.$apply(fn);
		        }
		    }
		}])
		.filter('tohtml', ['$sce', function($sce) {
		    return function(val) {
		        return $sce.trustAsHtml(val);
		    };
		}]);
		app.controller('search', ['$scope', '$http', 'safeApply', function($scope, $http, safeApply) {

			var form = $('#frmFiltSearch');
			$scope.currentPage = $('input[name=curtpage]').val() ? parseInt($('input[name=curtpage]').val()) : '';
			$scope.maxSize = 6;
			$scope.pageNum = $('.show-area').attr('totpage') != '' ? 
				parseInt($('.show-area').attr('totpage')) * 10 : 0;
			$scope.pageInput = '';

			$scope.$watch('pageInput', function(val) {
				$scope.pageInput = val.replace(/\D/gi,'');
			});

			$scope.pageChanged = function(opt) {

				if(opt && opt == 1)
					if($scope.pageInput != '' && $scope.pageInput > 0 && $scope.pageInput != $scope.currentPage && $scope.pageInput <= $scope.pageNum / 10) {

						form.find('input[name=curtpage]').attr('value', $scope.pageInput);
						form.submit();
					}
				if(!opt) {
					form.find('input[name=curtpage]').attr('value', $scope.currentPage);
					form.submit();
				}
			}
		}])
		.directive('pdt', ['$http', function($http) {//每个商品

			return {

				restrict: 'A',
				scope: true,
				link: function(scope, el, attrs) {

					var jbox = '';
					scope.pdtId = attrs.pdt ? attrs.pdt : '';
					el.bind('mouseenter', function() {

						if(el.hasClass('isQuery') || !islogin) 
							return false;
						else
							$http({
								method: 'POST',
								url: '/control/command',
								params: {
									'command': 'com.jackrain.ds.CommESCollectionQuery',
									'getjson': true,
									'ES_GOODS_ID': scope.pdtId
								}
							}).then(function(res) {

								if(res.data.code == 0) {
									$(el).addClass('isQuery');
									if(res.data.message.goodsArray && res.data.message.goodsArray.length > 0)
										$(el).find('i.get').html('已收藏').addClass('collected');
								}
							});
					});

					$(el).find('i.get').click(function() {

						if(islogin) {
							jbox != '' ? jbox.destroy() : '';
							$http({
								method: 'POST',
								url: '/control/command',
								params: {
									'command': 'com.jackrain.ds.CommESGoodsCollectionSave',
									'getjson': true,
									'ES_GOODS_ID': scope.pdtId
								}
							}).then(function(res) {

								if(res.data.code == 0 && res.data.value == 1) {

									jbox = new jBox('Notice', {
										autoClose: 1500,
										addClass: 'font-b',
										content: '收藏成功',
										position: {
											x: 'center',
											y: 50
										}
									});
									$(el).children('i.get').html('已收藏').addClass('collected');
								} else if(res.data.code == 0 && res.data.value == 0) {
									jbox = new jBox('Notice', {
										autoClose: 1500,
										addClass: 'font-b',
										content: '取消收藏成功',
										position: {
											x: 'center',
											y: 50
										}
									});
									$(el).children('i.get').html('收藏').removeClass('collected');
								}
									
							});
						} else
							var login = new jBox('Modal', {					    
							    title: '用户登录',
							    closeOnClick: 'body',
							    closeButton: 'title',
							    ajax: {
								    url: '/esvml/login/login.htm',
								    reload: false,
								    setContent: true,
								    spinner: true
								},
							    onCloseComplete: function() {
							    	login.destroy();
							    }
							}).open();
					});
				}
			}
		}]);

		angular.element(document).ready(function() {
			angular.bootstrap(document, ['app']);
			
			if($('.link-area').attr('isguest') != 'true') islogin = true;
		});
		
		return app;
	});
})();